package cn.zzuisa.bluebox.biz.device.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import cn.zzuisa.bluebox.base.facade.PageRequest;
import cn.zzuisa.bluebox.base.facade.R;
import cn.zzuisa.bluebox.biz.device.entity.Device;
import cn.zzuisa.bluebox.biz.device.mapper.DeviceMapper;
import cn.zzuisa.bluebox.biz.device.service.DeviceService;
import cn.zzuisa.bluebox.biz.device.service.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Ao
 * @since 2018-11-30
 */
@RestController
@Api(tags = "Device")
@RequestMapping("/device/dev")
public class DeviceController {


    @Autowired
    private DeviceService deviceService;
    /**
     * 其实是可以直接从这一层直接操作MAPper
     */
    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private WebSocketServer webSocketServer;


    @ApiOperation(value = "Query device device paging list")
    @PostMapping("/list")
    public R<IPage<Map<String, Object>>> getList(PageRequest pageRequest, String[] ids) {
        IPage<Device> page = new Page<>(pageRequest.getCurrent(), pageRequest.getSize());
        IPage<Map<String, Object>> devices = deviceMapper.pageDevices(page,ids);
        return R.ok(devices);
    }

    @ApiOperation(value = "Add devices", notes = "name required")
    @PostMapping("/add")
    public R<String> create(@RequestBody @Valid Device device) {
        device.setCreateTime(new Date());
        deviceService.save(device);
        return R.ok("add success");
    }
    @ApiIgnore
    @ApiOperation(value = "Update devices", notes = "id required")
    @PutMapping()
    public R<Device> update(@RequestBody Device device) {
        if (device == null ) {
            return R.error(device);
        }
        Device device1 = deviceService.selectDeviceByImei("a");
        device.setId(device1.getId());
        deviceService.updateById(device);
        return R.ok(device1);
    }
    @ApiIgnore
    @ApiOperation(value = "设备详情")
    @GetMapping("/{id}")
    public R getDetail(@PathVariable("id") Long id) {
        Device device = deviceService.getById(id);

        return R.ok(device);
    }

    @ApiIgnore
    @GetMapping("mapList")
    public R getMapList() {
        List<Device> l = deviceMapper.getMapList();
        return R.ok(l);
    }
    @ApiIgnore
    @ApiOperation(value = "查看设备详情")
    @GetMapping("/admin/{deviceId}")
    public R getDetailByAdmin(@PathVariable("deviceId") Long deviceId) {
        Device device = deviceService.getById(deviceId);
        if (device == null) {
            return R.error("传入设备ID不存在");
        }
        return R.ok(device);
    }
    @ApiIgnore
    @ApiOperation(value = "批量删除设备信息")
    @DeleteMapping()
    public R deleteByIds(Long[] ids) {
        System.out.println(ids);
        for (Long id : ids) {
            deviceService.removeById(id);
        }
        return R.ok("删除成功");
    }
    @ApiIgnore
    @GetMapping("exceptionDevice")
    public R getExceptionDevice() {
        List l = deviceMapper.getExceptionDeviceList();
        return R.ok(l);
    }

    /**
     * 普通用户查询授权给欸自己的设备
     * 管理员查询所有设备
     */

}
